package com.classic.utils;

import android.support.v4.app.FragmentActivity;

import com.classic.content.contents;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author  yzx
 * @Data    2019-3-18
 * @mail    1165747413@qq.com
 *
 * 铃声抓取工具类
 * 使用方法
 *   1、 MusicUtils musicUtils  =  new MusicUtils(this);
 *   ps : this = = implements MusicUtils.OnListener
 *
 *   2、 musicUtils.initListData(final int position, final FragmentActivity activity);
 *   ps  :  返回数据 succeed(int code, List<MusicBen> ben) or error(int error)
 *
 *   3、 musicUtils.initSearchData(final String search, final FragmentActivity activity);
 *   ps  :  返回数据 succeed(int code, List<MusicBen> ben) or error(int error)
 *
 *   4、musicUtils.initDownloadUrl(final String url, final FragmentActivity activity);
 *   ps  :  返回数据  succeedDetails(int code, String url) or error(int error)
 *
 *   ps 方法initDownloadUrl里面需要的 url 是有  initListData 与 initSearchData 方法返回数据 MusicBen.url
 */
public class MusicUtils {
    private OnListener mListener;

    /**
     * 初始化工具类 用于回调方法
     *
     * @param mListener 当前查询结果回调
     */
    public MusicUtils(OnListener mListener) {
        this.mListener = mListener;
    }

    /**
     * 回调接口
     */
    public interface OnListener {
        /**
         * @param code 200 成功 201 最后一页
         */
        void succeed(int code, List<MusicBen> ben);

        /**
         * @param code 200 成功  202 错误了
         */
        void succeedDetails(int code, String url);

        /**
         * @param error 错误代码 暂时未定
         */
        void error(int error);
    }

    /**
     * 请求列表数据方法
     *
     * @param position 当前页面下标
     * @param activity 当前页面引用
     */
    public void initListData(final int position, final FragmentActivity activity) {
        new Thread() {
            @Override
            public void run() {
                Document doc;//contents mu_1
                try {//
                    doc = Jsoup.connect(contents.url_music.replace("小苹果", String.valueOf(position))).get();
                    Elements elementsByClass = doc.getElementsByClass("contents mu_1");
                    Elements a = elementsByClass.get(0).select("a");
                    final ArrayList<MusicBen> objects = new ArrayList<>();
                    for (int i = 0; i < a.size(); i++) {
//                        Log.d("------------", a.get(i).attr("href") + "\n --------");
//                        Log.d("------------", a.get(i).text() + "\n --------");
                        MusicBen musicBen = new MusicBen();
                        musicBen.name = a.get(i).text();
                        musicBen.url = a.get(i).attr("href");
                        objects.add(musicBen);
                    }
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (null != mListener)
                                mListener.succeed(objects.size() > 99 ? 200 : 201, objects);
                        }
                    });

                } catch (IOException e) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (null != mListener) mListener.error(404);
                        }
                    });
                }
            }
        }.start();
    }

    /**
     * 搜索页面调用方法
     *
     * @param search   需要搜索的内容
     * @param activity 当前页面引用
     */
    public void initSearchData(final String search, final FragmentActivity activity) {
        new Thread() {
            @Override
            public void run() {
                Document doc;//contents mu_1
                try {//
                    doc = Jsoup.connect(contents.url_search_music.replace("小苹果", String.valueOf(search))).get();
                    Elements elementsByClass = doc.getElementsByClass("contents mu_1");
                    Elements a = elementsByClass.get(0).select("a");
                    final ArrayList<MusicBen> objects = new ArrayList<>();
                    for (int i = 0; i < a.size(); i++) {
//                        Log.d("------------", a.get(i).attr("href") + "\n --------");
//                        Log.d("------------", a.get(i).text() + "\n --------");
                        MusicBen musicBen = new MusicBen();
                        musicBen.name = a.get(i).text();
                        musicBen.url = a.get(i).attr("href");
                        objects.add(musicBen);
                    }
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (null != mListener)
                                mListener.succeed(objects.size() > 99 ? 200 : 201, objects);
                        }
                    });

                } catch (IOException e) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (null != mListener) mListener.error(404);
                        }
                    });
                }
            }
        }.start();
    }

    /**
     * 详情页面 获取当前播放引用的网址
     *
     * @param url      initListData 与 initSearchData 返回数据里面的url
     * @param activity 当前页面引用
     */
    public void initDownloadUrl(final String url, final FragmentActivity activity) {
        new Thread() {
            @Override
            public void run() {
                Document doc;//contents mu_1
                try {
                    doc = Jsoup.connect(url.contains("http") ? url : contents.url_details_music + url).get();
                    Elements elementsByClass = doc.getElementsByClass("playercode");
                    final Elements a = elementsByClass.get(0).select("param");
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (null == mListener) return;
                            if (a.size() > 1) {
                                mListener.succeedDetails(200, a.get(1).attr("value").replace("&autostart=1", "").replace("mp3=", ""));
                            } else {
                                mListener.succeedDetails(202, "http://m3.13400.com:9888/2016n/54/102614.mp3");
                            }
                        }
                    });

                } catch (IOException e) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mListener.error(404);
                        }
                    });
                }
            }
        }.start();
    }


}
